Skip to content

C/C++: Disable constant folding of address-vs-null comparisons#21258

Open
IdrissRio wants to merge 4 commits intomainfrom
idrissrio/cpp/edg/unfolding
Open

C/C++: Disable constant folding of address-vs-null comparisons#21258
IdrissRio wants to merge 4 commits intomainfrom
idrissrio/cpp/edg/unfolding

Conversation

@IdrissRio
Copy link
Contributor

@IdrissRio IdrissRio commented Feb 3, 2026

The EDG frontend constant-folds address-vs-null comparisons (e.g., &var == NULL), causing the IR to lose the VariableAddressInstruction.

This PR adds a containsAddressOf predicate used in isIRConstant so these expressions are expanded instead of collapsed.

@github-actions github-actions bot added the C++ label Feb 3, 2026
@IdrissRio IdrissRio force-pushed the idrissrio/cpp/edg/unfolding branch 3 times, most recently from 15ff5d6 to a7c6736 Compare February 4, 2026 08:42
@IdrissRio IdrissRio force-pushed the idrissrio/cpp/edg/unfolding branch from a7c6736 to 8cfa0a3 Compare February 4, 2026 09:16
@IdrissRio IdrissRio added the no-change-note-required This PR does not need a change note label Feb 4, 2026
@IdrissRio IdrissRio marked this pull request as ready for review February 6, 2026 14:46
@IdrissRio IdrissRio requested a review from a team as a code owner February 6, 2026 14:46
Copilot AI review requested due to automatic review settings February 6, 2026 14:46
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses an issue where the EDG frontend constant-folds address-vs-null comparisons (e.g., &var == NULL), causing the intermediate representation (IR) to lose VariableAddressInstruction nodes. The fix adds a containsAddressOf predicate to prevent constant folding of expressions containing address-of operators, ensuring that address operations remain visible in the IR for proper analysis.

Changes:

  • Added containsAddressOf predicate to detect expressions containing address-of operations
  • Modified isIRConstant to exclude expressions containing address-of operators
  • Added comprehensive test coverage for address-vs-null comparisons with global, local, and parameter variables

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.

File Description
cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll Added containsAddressOf predicate and integrated it into isIRConstant to prevent constant folding of expressions containing address-of operators
cpp/ql/test/library-tests/ir/address_constant_folding/test.c Comprehensive test file with various address-vs-null comparison scenarios covering global, local, and parameter variables
cpp/ql/test/library-tests/ir/address_constant_folding/variable_addresses.ql Test query to verify VariableAddressInstruction nodes are present in the IR
cpp/ql/test/library-tests/ir/address_constant_folding/variable_addresses.expected Auto-generated test expectations file (empty, will be populated when tests run)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C++ no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant